package localFiles;

/***
 * @author Ragnvald Fuglestein
 */

import java.io.File;

import baseStructure.Album;
import baseStructure.Artist;
import baseStructure.Log;
import baseStructure.SortedList;
import baseStructure.Track;

import callback.ImportArtistCallback;

import library.LibraryCollectArtists;
import localFiles.PathExtractionSubPattern.Types;

// TODO: Rewrite the whole thing

public class LocalCollectArtists extends LibraryCollectArtists {
	private Artist artist = null;
	private Album album = null;
	private String artistname = null;
	private String albumname = null;
	private String trackname = null;
	private int albumyear = -1;
	private int tracknum = -1;
	
	public LocalCollectArtists(final ImportArtistCallback callback, final File pickup, final PathExtractionPattern pattern) {
		super(callback);
		artistlist = new SortedList<Artist>(pickup.listFiles().length, 50);
		new Thread("LocalCollectArtists") {
			public void run() {
				recursiveHelper(pickup, 0, pattern);
				Log.i(String.format("Imported " + artistlist.size() + " artists from " + pickup.toString()));
				setCompleted();
			}
		}.start();
	}
	
	
	private final void recursiveHelper(File file, int level, PathExtractionPattern pattern) {
		PathExtractionSubPattern subpattern = pattern.fields.get(level);
		File[] contents = file.listFiles();
		boolean artistNameSet = false;
		boolean albumNameSet = false;
		boolean trackNameSet = false;
		boolean albumYearSet = false;
		boolean trackNumSet = false;
		for (File f : contents) {
			artistNameSet = false;
			albumNameSet = false;
			trackNameSet = false;
			albumYearSet = false;
			trackNumSet = false;
			if (!subpattern.match(f.getName())) {
				//Log.d(f.getName() + " != " + subpattern.data);
				continue;
			} else {
				//Log.d(f.getName() + " == " + subpattern.data);
			}
			Types thisType = subpattern.type;
			if (artistname == null && subpattern.containsArtistName) {
				artistname = subpattern.getArtistName();
				artistNameSet = true;
			}
			if (albumname == null && subpattern.containsAlbumName) {
				albumname = subpattern.getAlbumName();
				albumNameSet = true;
			}
			if (trackname == null && subpattern.containsTrackName) {
				trackname = subpattern.getTrackName();
				trackNameSet = true;
			}
			if (albumyear == -1 && subpattern.containsAlbumYear) {
				albumyear = subpattern.getAlbumYear();
				albumYearSet = true;
			}
			if (tracknum == -1 && subpattern.containsTrackNum) {
				tracknum = subpattern.getTrackNum();
				trackNumSet = true;
			}
			String extension;
			if (f.isFile() && // Yep. It's really ugly!
					level == pattern.level - 1 && 
					trackname != null && 
					subpattern.containsExtension &&
					((extension = subpattern.getExtension()).equals("mp3") ||
							extension.equals("flac") ||
							extension.equals("wav") ||
							extension.equals("wma") ||
							extension.equals("m4a") ||
							extension.equals("ape") ||
							extension.equals("ogg")
					) &&
					(artist != null || artistname != null)
			){
				if (artist == null) {
					artist = new Artist(artistname);
				}
				
				if (album == null) {
					if (albumname != null) {
						if (albumyear != -1) {
							album = new Album(artist, albumname, albumyear);
						} else { 
							album = new Album(artist, albumname);
						}
						artist.addAlbum(album);
					}
				}
				Track track;
				if (album == null) {
					if (tracknum == -1) track = new Track(artist, trackname);
					else track = new Track(artist, trackname, tracknum);
					artist.addTrack(track);
				} else {
					if (tracknum != -1) track = new Track(artist, album, trackname, tracknum);
					else track = new Track(artist, album, trackname);
					album.addTrack(track);
				}
				track.addAddon(new LocalTrackAddon(f));
				if (!artistlist.contains(artist)) artistlist.add(artist);
			} else if (f.isDirectory()) {
				recursiveHelper(f, level + 1, pattern);
				//Log.d("recurse " + f.getName() + " - " + level + "/" + (pattern.level - 1));
				if (thisType == PathExtractionSubPattern.Types.artist && artist != null) {
					artist.addAddon(new LocalArtistAddon(f));
				}
				if (thisType == PathExtractionSubPattern.Types.album && album != null) {
					album.addAddon(new LocalAlbumAddon(f));
				}
			}
			if (artistNameSet) {
				artistname = null;
				artist = null;
				albumname = null;
				album = null;
			}
			if (albumNameSet) {
				albumname = null;
				album = null;
			}
			if (trackNameSet) trackname = null;
			if (albumYearSet) albumyear = -1;
			if (trackNumSet) tracknum = -1;
		}
	}
}
